//
//  LYExtListCell.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/8/1.
//

import UIKit

class LYExtListCell: UITableViewCell {
    
    var row = 0
    
    var didClickButtonClourse: ((ContactsSeptenier, Int) -> Void)?
    var contactsSeptenier: ContactsSeptenier? = nil

    static let leftRMargin = 16.0
    static let top = 15.0
    private lazy var backgroundImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        iv.clipsToBounds = false
        iv.image = UIImage(named: "personal_list_bg")
        iv.isUserInteractionEnabled = true
        return iv
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 18)
        label.textAlignment = .center
        label.numberOfLines = 0
        label.textColor = UIColor(hex: "#000000")
        label.textAlignment = .left
        label.text = "saappppss"
        return label
    }()
    
    private lazy var textField: LYTextField = {
        let textField = LYTextField(backgroundImage: nil)
        return textField
    }()
    
    lazy var arrowButton: UIButton = {
        let button = UIButton(type: .custom)
        button.addTarget(self, action: #selector(arrowButtonAction(_:)), for: .touchUpInside)
        return button
    }()
    
    private lazy var arrowImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        iv.clipsToBounds = false
        iv.image = UIImage(named: "arrow_alert")
        return iv
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        
        commonInitSubviews()
    }
    
    func commonInitSubviews() {
        contentView.addSubview(backgroundImageView)
        backgroundImageView.addSubview(titleLabel)
        backgroundImageView.addSubview(textField)
        backgroundImageView.addSubview(arrowButton)
        backgroundImageView.addSubview(arrowImageView)
        setLayoutSubviews()
    }
    
    func setLayoutSubviews() {
        backgroundImageView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(LYExtListCell.top)
            make.bottom.equalToSuperview()
            make.leading.trailing.equalToSuperview().inset(LYExtListCell.leftRMargin)
        }

        titleLabel.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(14)
            make.leading.trailing.equalToSuperview().inset(10)
        }
        
        textField.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(40)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalToSuperview()
        }
        
        arrowButton.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(40)
            make.leading.trailing.equalTo(titleLabel)
            make.bottom.equalToSuperview()
        }
        
        arrowImageView.snp.makeConstraints { make in
            make.centerY.equalTo(textField)
            make.trailing.equalToSuperview().offset(-16)
        }

    }
    
    func configure(with contactsSeptenier: ContactsSeptenier, row: Int) {
        self.contactsSeptenier = contactsSeptenier
        self.row = row
        if row == 0 {
            if let shotization = contactsSeptenier.shotization {
                self.titleLabel.text = shotization
            }
           
            if let liehood = contactsSeptenier.liehood {
                self.textField.textField.placeholder = liehood
            }
       
            if let worryage = Int(contactsSeptenier.worryage ?? "-1"),
               let tredectures = contactsSeptenier.tredecture {
                
                let tredecture = tredectures.filter { $0.storyitious == "\(worryage)" }
                self.textField.textField.text = tredecture.first?.sarcling
            } else {
                self.textField.textField.text = ""
            }
        } else {
            if let fieldtion = contactsSeptenier.fieldtion {
                self.titleLabel.text = fieldtion
            }
            
            if let sportade = contactsSeptenier.sportade {
                self.textField.textField.placeholder = sportade
            }
            
            if let name = contactsSeptenier.sarcling,
               !name.isEmpty,
               let forgetence = contactsSeptenier.forgetence,
               !forgetence.isEmpty {
                self.textField.textField.text = name + "-" + forgetence
            } else {
                self.textField.textField.text = ""
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func arrowButtonAction(_ sender: UIButton) {
       
        guard let contactsSeptenier = contactsSeptenier else { return }
        didClickButtonClourse?(contactsSeptenier, self.row)
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    }
}
